using System;
using System.Collections.Generic;
using System.Text;
using OWED.Document;
using System.Xml;
using System.Drawing;
using System.Windows.Forms;
using ClientGUI.Modules.Drawing;

namespace ClientGUI
{

    /// <summary>
    /// This is for the para tag.  This is the only one which shows
    /// displayable pcdata so this has special code for handling text display.
    /// </summary>
	class Para:SimpleSquare, ITextAble
	{
		XmlNode my_config;
        
        
        Color my_bg_color;

        bool am_dirty = false;

		public Para(XmlNode doc, Module parent, 
			DisplayDocument host, XmlNode modconfig, 
            TagConfig tagconfig)
			: base(doc, parent, host, modconfig, tagconfig)
		{
            my_bottom_buffer = 2;
            my_top_buffer = 2;
            my_left_buffer = 15;
            my_right_buffer = 5;
            my_top_space = 0;
            my_bottom_space = 5;
			my_config = modconfig;

            if (modconfig != null)
            {
                XmlNode n = modconfig.SelectSingleNode("Background");
                String color = n.Attributes.GetNamedItem("color").Value;
                int red = Util.Convertors.HexToInt(color.Substring(1, 2).ToUpper());
                int green = Util.Convertors.HexToInt(color.Substring(3, 2).ToUpper());
                int blue = Util.Convertors.HexToInt(color.Substring(5, 2).ToUpper());
                my_bg_color = Color.FromArgb(red, green, blue);
                //Console.WriteLine(my_bg_color);
                //n = modconfig.SelectSingleNode("SideLine");
                //color = n.Attributes.GetNamedItem("color").Value;
                //int red1 = Util.Convertors.HexToInt(color.Substring(1, 2));
                //int green1 = Util.Convertors.HexToInt(color.Substring(3, 2));
                //int blue1 = Util.Convertors.HexToInt(color.Substring(5, 2));
                //my_side_color = Color.FromArgb(red1, green1, blue1);
            }
            else
            {
                my_bg_color = Color.White;
            }
		}

        public XmlNode Config { get { return my_config; } }

        /// <summary>
        /// Goes through all children and condenses them into TextDraw whenever
        /// possible.
        /// </summary>
        private void Condense()
        {
            List<Module> new_children = new List<Module>(); 
            List <IWordAble> wordable = new List<IWordAble>();
            for (int i = 0; i < Children.Count; ++i)
            {
                if ((Children[i] as IWordAble) != null)
                {
                    wordable.Add(Children[i] as IWordAble);
                }
                else
                {
                    if (wordable.Count > 0)
                    {
                        List<Word> words = new List<Word>();
                        for (int j = 0; j < wordable.Count; ++j)
                        {
                            for (int c=0; c < wordable[j].Length; ++c)
                                words.Add(wordable[j][c]);
                        }
                        TextDraw tdraw = new TextDraw(words.ToArray(), //UGLY HACK
                            this, my_host, new TagConfig(), ((Module)wordable[0]).Node,
                            wordable.Count);
                        new_children.Add(tdraw);

                        wordable = new List<IWordAble>();
                    }
                    new_children.Add(Children[i]);
                }
            }
            if (wordable.Count > 0)
            {                   
                List<Word> words = new List<Word>();
                for (int j = 0; j < wordable.Count; ++j)
                {
                    for (int c=0; c < wordable[j].Length; ++c)
                        words.Add(wordable[j][c]);
                }
                TextDraw tdraw = new TextDraw(words.ToArray(), //UGLY HACK
                    this, my_host, new TagConfig(), ((Module)wordable[0]).Node,
                    wordable.Count);
                new_children.Add(tdraw);
            }
            Children.Clear();
            for (int i = 0; i < new_children.Count; ++i)
                Children.Add(new_children[i]);
            am_dirty = false;
        }

        protected override void Insert(Module child, int at)
        {
            base.Insert(child, at);
            if ((child as IWordAble) != null)
                am_dirty = true;
        }
        protected override void Remove(int at)
        {
            base.Remove(at);
            if ((Children[at] as IWordAble) != null)
                am_dirty = true;
        }

		public override void Space(int x)
		{
            if (am_dirty)
                Condense();
            base.Space(x);
		}


		public bool Pure
		{
			get
			{
				foreach (Module kid in Children) {
					if ((kid as ITextAble) == null) {
						return false;
					}
				}
				return true;
			}
		}

		public String Text
		{
			get
			{
				StringBuilder text = new StringBuilder();
				foreach (Module kid in Children) {
					if ((kid as ITextAble) != null) {
						text.Append((kid as ITextAble).Text);
					}
				}
				text.Append("\n\n");
				return text.ToString();
			}
		}

    }
}
